package com.yidu.t299.dao.xp;
import com.yidu.t299.domain.xp.LogManage;
import com.yidu.t299.domain.xp.StudentManage;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.type.JdbcType;
import java.util.List;

/*
使用Mybatis逆向生成自动生成 后续自己更改
 */
@Mapper
public interface LogManageMapper {
    /**
     * 根据日志编号删除
     * @param logId 日志编号
     * @return
     */
    @Delete({
        "delete from log_manage",
        "where log_id = #{logId,jdbcType=INTEGER}"
    })
    int deleteByPrimaryKey(Integer logId);

    /**
     * 增加日志信息
     * @param record 日志对象
     * @return 影响的行数(返回1则成功)
     */
    @Insert({
        "insert into log_manage (log_id, student_id, ",
        "log_acquit, log_date)",
        "values (#{logId,jdbcType=INTEGER}, #{studentId,jdbcType=INTEGER}, ",
        "#{logAcquit,jdbcType=VARCHAR}, #{logDate,jdbcType=VARCHAR})"
    })
    int insert(LogManage record);

    /**
     * 根据日志编号查询指定信息
     * @param logId 日志编号
     * @return
     */
    @Select({
        "select",
        "log_id, student_id, log_acquit, log_date",
        "from log_manage",
        "where log_id = #{logId,jdbcType=INTEGER}"
    })
    @Results(id = "LogManageMap",value = {
        @Result(column="log_id", property="logId", jdbcType=JdbcType.INTEGER, id=true),
        @Result(column="student_id", property="studentId", jdbcType=JdbcType.INTEGER),
        @Result(column="log_acquit", property="logAcquit", jdbcType=JdbcType.VARCHAR),
        @Result(column="log_date", property="logDate", jdbcType=JdbcType.VARCHAR)
    })
    LogManage selectByPrimaryKey(Integer logId);

    /**
     * 按条件分页查询日志数据集合
     * @param rows 每页的行数
     * @param page 页码
     * @param condition 条件参数
     * @return 日志对象集合
     */
    @Select("SELECT * FROM log_manage cm " +
            "JOIN student_manage c " +
            "ON cm.student_id=c.student_id where 1=1 ${condition} limit #{page},#{rows}")
    @Results({
            @Result(column = "log_id",property = "logId",jdbcType = JdbcType.INTEGER, id = true),
            @Result(column = "student_id",property = "studentId",jdbcType = JdbcType.INTEGER),
            @Result(column = "log_acquit",property = "logAcquit",jdbcType = JdbcType.VARCHAR),
            @Result(column = "log_date",property = "logDate",jdbcType = JdbcType.VARCHAR),
            @Result(column = "student_name",property = "studentName",jdbcType = JdbcType.VARCHAR)
    })
    List<LogManage> selectByPage(@Param("rows") int rows,@Param("page") int page,@Param("condition") String condition);

    /**
     * 按条件查询log_manage表中数据行数因为涉及了多表
     * 所以这里也需要多表联查不然无法设置条件
     * @param condition 条件参数
     * @return 数据行数
     */
    @Select("SELECT count(*)  FROM log_manage cm " +
            "JOIN student_manage c " +
            "ON cm.student_id=c.student_id where 1=1 ${condition}")
    int selectCount(@Param("condition") String condition);

    /**
     * 根据日志信息修改其数据
     * @param record
     * @return
     */
    @Update({
        "update log_manage",
        "set student_id = #{studentId,jdbcType=INTEGER},",
          "log_acquit = #{logAcquit,jdbcType=VARCHAR},",
          "log_date = #{logDate,jdbcType=VARCHAR}",
        "where log_id = #{logId,jdbcType=INTEGER}"
    })
    int updateByPrimaryKey(LogManage record);

    //以下是获取下拉框数据的方法
    /**
     * 得到所有学员名称信息
     * @return 学员名称集合
     */
    @Select("select student_name from student_manage")
    @Results({
            @Result(id = true,column = "student_name",property = "studentName")
    })
    List<StudentManage> selectAllGetStudent();

    /**
     * 针对日志表(log_manage)进行操作
     * @param studentName 学员名称
     * @return 影响的行数(>1:成功)
     */
    @Select("select student_id from student_manage where student_name=#{studentName}")
    int selectByStudentName(@Param("studentName") String studentName);
}